<?php

namespace app\api\controller\company;

use app\api\controller\Baseuser;
use app\common\model\company\Score as ScoreDB;
use app\common\model\company\Company;
use app\common\model\user\User as UserModel;
use Exception;
use think\facade\Request;
use think\response\Json;

class Score extends Baseuser
{   

    /**
     * 创建产废单位规范化自查
     * 产废单位只能创建自己的  其他人员可以创建所有单位的
     * @return [type] [description]
     */
    public function create(): Json
    {
        $data = input('data');
        $sdata = json_decode($data,true);
        $cid = input('id');
        $company = Company::get($cid);
        if(empty($cid)||empty($company)){
            return myerr('没找到对应企业');
        }else{
            if($company['UserId']!==$this->user_id){
                //判断是否为业务员
                $user = UserModel::get($this->user_id);
                if($user['Role']=='101'){
                    return myerr('没有权限');
                }
            }
        }

        if (count($sdata) !== 15) {
            return myerr('参数错误','','goback');
        }
        
        try {
            $count_score = input('count_score');
            $adata = [
                'Source' => $count_score,
                'CreateTime' => date('Y-m-d H:i:s'),
                'UId' => $this->user_id,
                'CompanyId'  => $cid,
                'Info' => $data
            ];
            $list = ['CompanyId','UserName','UserTel','TouchTime','TouchUserName','TouchUserTel','ZhuYaoQingKuang','CompanyType','Level'];
            foreach ($list as $key => $value) {
                $sb = input($value);
                if(!empty($sb)){
                    $adata[$value] = trim($sb);
                }
            }

            $id = ScoreDB::insertGetId($adata);
            if (!$id) {
                throw new Exception("添加失败");
            }
        } catch (Exception $e) {
            return myerr($e->getMessage());
        }
        return myok([
            'id' => intval($id)
        ]);
    }

    /**
     * 修改自查表
     * 只有创建表的人可以修改
     * @return [type] [description]
     */
    public function reinfo(): Json
    {
        $data = input('data');
        $sdata = json_decode($data,true);
        $cid = input('id');
        $score = ScoreDB::get($cid);
        if(empty($cid)||empty($score)){
            return myerr('没找到对应自查表');
        }else{
            if($score['UId']!==$this->user_id){
                //判断是否为业务员
                return myerr('没有权限');
            }
        }
        if (count($sdata) !== 15) {
            return myerr('参数错误','','goback');
        }
        $count_score = input('count_score');
        //涉及到需要修改的信息
        //成绩信息
        $score->Source=$count_score;
        //数据信息
        $score->Info = $data;
        //修改时间
        $score->UpdateTime = date('Y-m-d H:i:s',time());
        $list = ['UserName','UserTel','TouchTime','TouchUserName','TouchUserTel','ZhuYaoQingKuang','CompanyType','Level'];
        foreach ($list as $key => $value) {
            $sb = input($value);
            $score->$value = trim($sb);
        }
        $score->save();
        return myok([
            'id' => intval($score->Id)
        ]);
    }

    public function list(): Json
    {
        $list = ScoreDB::where('UId', $this->user_id)->field([
            'Id', 'Source', 'CreateTime'
        ])->select();
        return myok($list);
    }

    public function info(): Json
    {   
        $id = Request::get('id', 0, 'intval');
        if ($id < 1) {
            return myerr('参数错误','','goback');
        }
        $info = ScoreDB::where('Id', $id)->find();
        if (!$info) {
            return myerr('参数错误','','goback');
        }
        $company = Company::where('CompanyId',$info['CompanyId'])->find();

        if($info['UId']==$this->user_id){
            //摸排表的创建者
        }else{
            //企业的负责人
            if($company['UserId']==$this->user_id){

            }elseif($company->guard_state){
                
            }else{
                return myerr('没有权限','','goback');
            }
        }
        $info['companyname'] = $company['Name'];
        return myok($info);
    }
}